﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 易容酒店管理系统
{
    public partial class Trade : Form
    {
        public Trade()
        {
            InitializeComponent();
        }

        private void butLookUp_Click(object sender, EventArgs e)
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "select Customer_data.Name,Customer_data.Document_type,Customer_data.CAAC,Customer_data.Telephone,Customer_data.requirements,Customer_data.Type,Customer_data.Mode,Guest_Room_Number.Guest_room_InTim,Guest_Room_Number.Guest_room_OutTim,Guest_Room_Number.Day,Guest_Room_Number.Money from Customer_data, GuestInfo,Guest_Room_Number where Customer_data.Name=Guest_Room_Number.Guest_information and GuestInfo.RoomID=Guest_Room_Number.RoomID and GuestInfo.RoomID='"+txtRoomID.Text+"' or Guest_Room_Number.Guest_information='"+ txtRoomID.Text + "' and GuestInfo.GusetInfo='有'";
                Sql += "and GuestInfo.GusetInfo='有'";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    decimal money;
                    SqlDataReader red = cmd.ExecuteReader();
                    if (red.HasRows)
                    {
                        red.Read();
                        txtName.Text = red["Name"].ToString();
                        cobPapers.Text = red["Document_type"].ToString();
                        txtID.Text = red["CAAC"].ToString();
                        txtTelephone.Text = red["Telephone"].ToString();
                        text.Text = red["requirements"].ToString();
                        comType.Text = red["Type"].ToString();
                        money = Convert.ToDecimal(red["Money"]);
                    }
                }
            }
        }
        /// <summary>
        /// 读取所设定的客房类型
        /// </summary>
        public void COMTYPE()
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            SqlConnection con = new SqlConnection(Link);
            string Sql = "select * from Guest_Room;";
            SqlDataAdapter Add = new SqlDataAdapter(Sql, con);
            DataSet set = new DataSet();
            Add.Fill(set, "Guest_Room");
            DataTable table = set.Tables["Guest_Room"];

            comType.DisplayMember = "GuestType";
            comType.DataSource = set.Tables["Guest_Room"];
        }

        private void Trade_Load(object sender, EventArgs e)
        {
            COMTYPE();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            int num = 0;
            using (SqlConnection con = new SqlConnection(Link))
            {
                
                string  Sql3= "update Customer_data set Type='"+comType.Text+"' where Name='" + txtName.Text + "';";
                using (SqlCommand cmd = new SqlCommand(Sql3, con))
                {
                    con.Open();
                    num += cmd.ExecuteNonQuery();
                    con.Close();
                }
               
            }
            string RoomID= Room();
            MessageBox.Show("已更换之" + Room() + "客房");
            using (SqlConnection con2 = new SqlConnection(Link))
            {
                string Sql = "update Guest_Room_Number set Type='" + comType.Text + "' , RoomID='" + RoomID+ "'where Guest_information='"+txtName.Text+"';";
                using (SqlCommand cmd = new SqlCommand(Sql, con2))
                {
                    con2.Open();
                    num += cmd.ExecuteNonQuery();
                    con2.Close();
                }
                string Sql2 = "update GuestInfo set GusetInfo='无', setting='未打扫' where RoomID='" + txtRoomID.Text + "';";
                using (SqlCommand cmd = new SqlCommand(Sql2, con2))
                {
                    con2.Open();
                    num += cmd.ExecuteNonQuery();
                    con2.Close();
                }
                string Sql4 = "update GuestInfo set GusetInfo='有', setting='已打扫' where RoomID='" + RoomID + "';";
                using (SqlCommand cmd = new SqlCommand(Sql4, con2))
                {
                    con2.Open();
                    num += cmd.ExecuteNonQuery();
                    con2.Close();
                }
            }
        }
        /// <summary>
        /// 分配客房
        /// </summary>
        /// <returns></returns>
        private string Room()
        {
            string ID = "";
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "select top 1 RoomID from GuestInfo where Type='" + comType.Text + "'and GusetInfo='无' and setting='已打扫' and Reserve='未预订';";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    ID = (string)cmd.ExecuteScalar();
                }
            }
            return ID;
        }

        private void picColse_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
